package net.borlis.processor.dao.entity;

import net.borlis.processor.dao.entity.common.AuditedEntity;
import net.borlis.processor.dao.entity.common.BaseEntity;
import net.borlis.processor.enumerated.TaskStatus;

import javax.persistence.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by borlis on 24.12.2014.
 */
@Entity
@Table(name = "task")
public class TaskEntity extends BaseEntity implements AuditedEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private BigInteger id;

    @ManyToOne
    @JoinColumn(name = "fk_abstract_task_id", nullable = false)
    private AbstractTaskEntity abstractTask;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "uuid", nullable = false)
    @org.hibernate.annotations.Type(type = "org.hibernate.type.PostgresUUIDType")
    private UUID uuid;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "received_on", nullable = false, columnDefinition = "DATETIME")
    private Date receivedOn;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "sent_on", nullable = false, columnDefinition = "DATETIME")
    private Date sentOn;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "last_chacked_on", nullable = false, columnDefinition = "DATETIME")
    private Date lastCheckedOn;

    @Basic
    @Column(name = "locked", nullable = false)
    private Boolean locked;

    @Column(name = "locked_by")
    private UUID lockedBy;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "locked_on", nullable = false, columnDefinition = "DATETIME")
    private Date lockedOn;

    @Basic
    @Column(name = "status", nullable = false)
    @Enumerated(EnumType.STRING)
    private TaskStatus status;

    @OneToMany(mappedBy = "task", fetch = FetchType.EAGER)
    private List<TaskParameterEntity> parameters;

    @OneToMany(mappedBy = "task", fetch = FetchType.EAGER)
    private List<TaskResultEntity> results;

    public TaskEntity() {
        super();
    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public AbstractTaskEntity getAbstractTask() {
        return abstractTask;
    }

    public void setAbstractTask(AbstractTaskEntity abstractTask) {
        this.abstractTask = abstractTask;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public Date getReceivedOn() {
        return this.receivedOn != null ? new Date(this.receivedOn.getTime()) : null;
    }

    public void setReceivedOn(Date receivedOn) {
        this.receivedOn = (receivedOn != null ? new Date(receivedOn.getTime()) : null);
    }

    public Date getSentOn() {
        return this.sentOn != null ? new Date(this.sentOn.getTime()) : null;
    }

    public void setSentOn(Date sentOn) {
        this.sentOn = (sentOn != null ? new Date(sentOn.getTime()) : null);
    }

    public Date getLastCheckedOn() {
        return this.lastCheckedOn != null ? new Date(this.lastCheckedOn.getTime()) : null;
    }

    public void setLastCheckedOn(Date lastCheckedOn) {
        this.lastCheckedOn = (lastCheckedOn != null ? new Date(lastCheckedOn.getTime()) : null);
    }

    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    public UUID getLockedBy() {
        return lockedBy;
    }

    public void setLockedBy(UUID lockedBy) {
        this.lockedBy = lockedBy;
    }

    public Date getLockedOn() {
        return this.lockedOn != null ? new Date(this.lockedOn.getTime()) : null;
    }

    public void setLockedOn(Date lockedOn) {
        this.lockedOn = (lockedOn != null ? new Date(lockedOn.getTime()) : null);
    }

    public TaskStatus getStatus() {
        return status;
    }

    public void setStatus(TaskStatus status) {
        this.status = status;
    }

    public List<TaskParameterEntity> getParameters() {
        if (parameters == null) {
            parameters = new ArrayList<>();
        }
        return parameters;
    }

    public void setParameters(List<TaskParameterEntity> parameters) {
        this.parameters = parameters;
    }

    public List<TaskResultEntity> getResults() {
        if (results == null) {
            results = new ArrayList<>();
        }
        return results;
    }

    public void setResults(List<TaskResultEntity> results) {
        this.results = results;
    }
}
